package com.medical.lsq.controller;

import com.github.pagehelper.PageInfo;
import com.medical.lsq.entity.SysDisease;
import com.medical.lsq.service.DiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/disease")
public class DiseaseController {

    @Autowired
    private DiseaseService diseaseService;
    /**
     * 获取慢病分类信息列表，包含模糊查询，分页
     * @param currentPage
     * @param limit
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/getDiseaseList")
    public String getDiseaseList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String diseaseName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<SysDisease> pageInfo = diseaseService.getDiseaseList(currentPage, limit, diseaseName);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", diseaseName);
        return "/disease/disease_list";
    }

    @RequestMapping("/openAdd")
    public String openadd() {return "/disease/disease_add";}

    @RequestMapping("/openEdit")
    public String openEdit(Integer id,Model model) {
        SysDisease disease = diseaseService.getDisease(id);

        model.addAttribute("disease", disease);
        return "/disease/disease_edit";
    }
    @RequestMapping("/add")
    public String add(SysDisease sysDisease,HttpServletRequest request, Model model){
        diseaseService.add(sysDisease);
        return getDiseaseList(1, 10, request, model);
    }

    @RequestMapping("edit")
    public String edit(SysDisease disease, HttpServletRequest request, Model model) {
        diseaseService.edit(disease);
        return getDiseaseList(1, 10, request, model);
    }

    @RequestMapping("delete")
    public String delete(Integer id, HttpServletRequest request, Model model) {
        diseaseService.delete(id);
        return getDiseaseList(1, 10, request,model);
    }

}
